﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PssWork1.Model;

namespace PssWork1
{
    public partial class AddUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            show();
            
        }

        private void show()
        {
            using (FAQDBEntities db = new FAQDBEntities())
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                var data = db.Topics.FirstOrDefault(p => p.TopicId == id);
                wt1.Text = data.Title;
                wt2.Text = data.Content;
                sj.Text = data.UpdateTime.ToString("yyyy年MM月dd日") + data.IPAddress;

                Repeater1.DataSource = db.Comments.Where(p => p.TopicId == id).ToList();
                Repeater1.DataBind();

            }
        }

        //删除问题的时候要先删除外键表回答表中的回答
        protected void Delete_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            using (FAQDBEntities db =new FAQDBEntities())
            {

                var data = db.Topics.FirstOrDefault(p => p.TopicId == id);
                db.Comments.RemoveRange(data.Comments);
                db.Topics.Remove(data);
                db.SaveChanges();
                ClientScript.RegisterStartupScript(this.GetType(), "成功", "alter('问题删除成功！');",true);
            }
        }
        

        protected void btn1_Click(object sender, EventArgs e)
        {
            string nr = shuru.Text;
            if (shuru.Text!="")
            {
                using (FAQDBEntities db=new FAQDBEntities())
                {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    Comment comment = new Comment()
                    {
                        TopicId = id,
                        Content = nr,
                        UpdateTime = DateTime.Now,
                        IPAddress = "::1"
                    };
                    db.Comments.Add(comment);
                    db.SaveChanges();
                    ClientScript.RegisterStartupScript(this.GetType(), "成功", "alter('添加成功！');", true);
                    Repeater1.DataSource = db.Comments.ToList();
                    Repeater1.DataBind();
                    shuru.Text = "";
                }
              
            }
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Index.aspx");
        }
        //删除回答

       

        protected void del_Command1(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);

            using (FAQDBEntities db = new FAQDBEntities())
            {
                var data = db.Comments.FirstOrDefault(p => p.CommentId == id);
                db.Comments.Remove(data);
                db.SaveChanges();
                ClientScript.RegisterStartupScript(this.GetType(), "成功", "alter('问题删除成功！');", true);
                show();

            }

        }
    }
}